<template>
  <view>
    <!-- 1. 轮播图 -->
    <swiper class="goods-swiper" indicator-dots circular>
      <swiper-item v-for="item in goodsDetail.pics" :key="item.pics_id">
        <image class="goods-swiper-image" :src="item.pics_big" mode="scaleToFill" @tap="imageTap(item.pics_big)" />
      </swiper-item>
    </swiper>
    <!-- 2. 文字信息 -->
    <view class="goods-info">
      <view class="goods-info-title">
        <view class="goods-info-price">{{ goodsDetail.goods_price }}</view>
        <view class="icon-group">
          <!-- 分享按钮可通过定位放到图标的上方，透明度设置为0 -->
          <u-icon name="share-square" size="44rpx"></u-icon>
          <!-- 小程序没有直接提供分享的 api，页面中需通过按钮组件实现分享功能 -->
          <button open-type="share">分享按钮</button>
        </view>
        <view class="icon-group">
          <u-icon name="star" size="44rpx"></u-icon>
        </view>
      </view>
      <view class="goods-info-name">
        {{ goodsDetail.goods_name }}
      </view>
    </view>
    <!-- uView 间隔槽组件，用一个灰色块隔开两个分区 -->
    <u-gap height="20rpx" bgColor="#F9F9F9"></u-gap>
    <!-- 3. 图文详情 -->
    <view class="goods_introduce">
      <view class="goods_introduce-title">图文详情</view>
      <!-- 富文本组件展示html标签 rich-text -->
      <!-- <rich-text class="goods_introduce-content" :nodes="goodsDetail.goods_introduce"></rich-text> -->
      <u-parse :content="goodsDetail.goods_introduce" :tagStyle="{
        img: 'height:auto;vertical-align: top;',
      }"></u-parse>
    </view>
    <!-- 4. 底部操作栏 -->
    <view class="bottom-bar">
      <view class="bottom-bar-fixed">
        <view class="icon-group">
          <u-icon name="kefu-ermai" color="#333" size="40rpx"></u-icon>
          <view>联系客服</view>
          <button open-type="contact">客服按钮</button>
        </view>
        <navigator url="/pages/cart/cart" open-type="switchTab" class="icon-group">
          <u-icon name="shopping-cart" color="#333" size="40rpx"></u-icon>
          <view>购物车</view>
        </navigator>
        <view class="btn" @tap="addCart">加入购物车</view>
        <view class="btn">立即购买</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      queryParams: {},
      goodsDetail: {}
    }
  },

  onLoad(options) {
    this.queryParams = options
    this.getGoodsDetail()
  },

  methods: {
    async getGoodsDetail() {
      const res = await uni.$u.http.get('/goods/detail', { params: this.queryParams })
      this.goodsDetail = res.data.message
    },
    imageTap(current) {
      const urls = this.goodsDetail.pics.map(item => item.pics_big)
      uni.previewImage({
        urls,
        current
      })
    },
    addCart() {
      console.log(this.$store.state.cartList)
      const cartList = [...this.$store.state.cartList]
      const index = cartList.findIndex(v => v.goods_id === this.queryParams.goods_id)
      if (index > -1) {
        cartList[index].goods_count++
      } else {
        // 🟠不存在的商品，前添加到列表中
        cartList.unshift({
          // 商品id
          goods_id: this.queryParams.goods_id,
          // 商品图片
          goods_small_logo: this.goodsDetail.goods_small_logo,
          // 商品名称
          goods_name: this.goodsDetail.goods_name,
          // 商品价格
          goods_price: this.goodsDetail.goods_price,
          // --- 购物车列表专属的字段 ---
          // 商品数量
          goods_count: 1,
          // 商品选中
          goods_select: true,
        })
      }
      // 🔔分支判断外，更新数据到 Vuex 中
      this.$store.commit('setCartList', cartList)
      // 🔔分支判断外，提示用户
      uni.showToast({ icon: 'success', title: '添加购物车成功', mask: true })
    }
  },
}
</script>

<style lang="scss">
// 返回上一页按钮
.navigator-back {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 30rpx;
  top: 50rpx;
  z-index: 99;
  display: flex;
  justify-content: center;
  align-items: center;
}

.goods-swiper {
  width: 750rpx;
  height: 750rpx;

  .goods-swiper-image {
    width: 750rpx;
    height: 750rpx;
  }
}

// 商品文字信息
.goods-info {
  background-color: #fff;
  padding: 20rpx;

  .goods-info-title {
    display: flex;
    padding-bottom: 20rpx;

    .goods-info-price {
      color: #eb4450;
      flex: 1;

      // 前面的价格符号
      &::before {
        content: '￥';
        font-size: 80%;
        margin-right: 5rpx;
      }
    }

    // 图标组
    .icon-group {
      width: 44rpx;
      height: 44rpx;
      position: relative;
      margin-left: 40rpx;

      // 图标上方有个定位的按钮
      button {
        position: absolute;
        width: 44rpx;
        height: 44rpx;
        left: 0;
        top: 0;
        opacity: 0;
      }
    }
  }

  // 商品文字
  .goods-info-name {
    font-size: 30rpx;
    line-height: 1.5;
  }
}

.goods_introduce {
  .goods_introduce-title {
    font-size: 28rpx;
    height: 100rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1rpx solid #eee;
  }

  .goods_introduce-content {

    // 通过类名给富文本图片修改样式
    .img {
      vertical-align: middle;
    }
  }
}

// 底部操作栏
.bottom-bar {
  // 占位用
  height: 100rpx;

  // 定位的操作栏
  .bottom-bar-fixed {
    display: flex;
    align-items: center;
    height: 94rpx;
    background-color: #fff;
    border-top: 1px solid #ddd;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;

    // 注意这里客服图标和按钮的组合处理
    .icon-group {
      flex: 1;
      font-size: 22rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      position: relative;

      button {
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
      }
    }

    .btn {
      width: 196rpx;
      height: 60rpx;
      border-radius: 30rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fcaa23;
      color: #fff;
      margin: 0 10rpx;
      font-size: 26rpx;

      &:last-child {
        background-color: #ea4350;
      }
    }
  }
}
</style>